/*
 * @Author: wangwendie
 * @Date: 2023-07-25 17:58:29
 * @LastEditors: wangwendie
 * @Description: 娱乐模块
 */
import { searchMovies } from "../api/fun.js";
import { fortune } from "../api/proxyApi.js";
import BaseClass from "./baseclass.js";

class Fun extends BaseClass {
  constructor(message, userinfo, mentionText) {
    super(message, userinfo, mentionText);
  }

  fortune = async (ruleText) => {
    if (!this.activateRule(ruleText)) return;
    let mentionText = this.mentionText;
    let consName = mentionText.substr(0, 3);
    let type = '';
    let mes = '';
    const handleFortuneText = (type, info) => {
      let { name, date, health, money, work, love, all } = info;
      switch (type) {
        case 'today':
        case 'tomorrow':
          var { color, number, QFriend, summary } = info;
          date = info.datetime;
          break;
        case 'week':
          work = info.job;
          break;
        case 'month':
          health = health.substr(3);
          money = money.substr(3);
          work = work.substr(4);
          love = love.substr(4);
          all = all.substr(4);
          break;
        case 'year':
          love = love[0];
          health = health[0];
          work = info.career[0];
          money = info.finance[0];
          all = info.mima.text[0];
          break;
        default:
          break;
      }

      return `${name}\n日期：${date}\n健康：${health}\n财运：${money}\n事业：${work}\n爱情：${love}\n${(all ? `总运势：${all}` : '')}\n${(color ? `幸运数字：${number}\n幸运颜色：${color}\n速配星座：${QFriend}` : '')}\n${(summary ? `运势概述：${summary}` : '')}\n`;
    }

    if (mentionText.includes('今日')) {
      type = 'today';
    } else if (mentionText.includes('明日')) {
      type = 'tomorrow';
    } else if (mentionText.includes('本周')) {
      type = 'week';
    } else if (mentionText.includes('本月')) {
      type = 'month';
    } else if (mentionText.includes('今年')) {
      type = 'year';
    }

    if (type === '' || consName[2] !== '座') {
      mes = '请说：XX座XX运势（例：金牛座今日(/本周/本月/今年)运势)';
    } else {
      let res = await fortune({ consName, type });
      // console.log(res);
      if (res.result.error_code !== 0) {
        mes = '星座运势查询失败';
      } else {
        mes = handleFortuneText(type, res.result);
      }
    }

    await this.message.say(mes);
  }

  searchMovies = async (ruleText) => {
    if (!this.activateRule(ruleText)) return;

    let useMessage = this.mentionText.replace(new RegExp(ruleText), "");
    const moviesTemplate = (searchMoviesData) =>{
      let StringTemplate = "";
      searchMoviesData.forEach((element, index) => {
        StringTemplate += `${index + 1}、${element.name}\n 链接地址：${element.url} \n\n`
      });
      return StringTemplate.slice(0, -2);
    }
    try {
      const searchMoviesData = await searchMovies({ vague_name: useMessage.trim() });
      if(searchMoviesData.result.length == 0){
        await this.message.say("未搜索到该视频，视频链接应该还没有入库\n或者\n查看一下你的输入是否有误");
        return false;
      }
      await this.message.say(moviesTemplate(searchMoviesData.result));
    } catch (err) {
      await this.message.say(`搜索失败`);
    }
  }
}

export default Fun